package otherapi.itext.itextpdf;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 4/12/2022 8:48 PM
 */
public class ItextPdfUtils {

    private static final Logger logger = LoggerFactory.getLogger(ItextPdfUtils.class);
    public static final int TWENTY = 20;
    public static final int Thirty = 30;
    public static final int TWELVE = 12;

    /**
     * 中文字体,解决中文不能显示问题
     */
    public static BaseFont baseFontChinese;
    public static Font font;
    public static Font blueFont;
    public static Font smallThreeFont;
    public static Font fiveFont;
    public static Font smallFiveFont;
    public static Font smallSixFont;
    public static Font greenFont;
    public static Font messFont;
    public static Font titleFont;

    static {
        try {
            baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            font = new Font(baseFontChinese);
            font.setSize(10);
            blueFont = new Font(baseFontChinese);
            blueFont.setColor(BaseColor.BLUE);
            blueFont.setSize(5);
            smallThreeFont = new Font(baseFontChinese, TWENTY);
            smallThreeFont.setColor(BaseColor.BLACK);
            fiveFont = new Font(baseFontChinese);
            fiveFont.setColor(BaseColor.BLACK);
            fiveFont.setSize(10.5f);
            smallFiveFont = new Font(baseFontChinese);
            smallFiveFont.setColor(BaseColor.BLACK);
            smallFiveFont.setSize(9);
            smallSixFont = new Font(baseFontChinese);
            smallSixFont.setColor(BaseColor.BLACK);
            smallSixFont.setSize(6.5f);
            greenFont = new Font(baseFontChinese, TWELVE, Font.BOLD);
            greenFont.setColor(BaseColor.BLACK);
            messFont = new Font(baseFontChinese, 12);
            messFont.setColor(BaseColor.BLACK);
            titleFont = new Font(baseFontChinese, 14, Font.BOLD);
            titleFont.setColor(BaseColor.BLACK);
        } catch (Exception e) {
            logger.error("Error Occur:{0}", e);
        }
    }

    /**
     * pdf 页面生成
     *
     * @param writer
     * @param yeMei
     * @param fontSize
     * @param pageSize
     */
    public static void setFooter(PdfWriter writer, String yeMei, int fontSize, Rectangle pageSize) {
        ItextPdfHeaderFooter headerFooter = new ItextPdfHeaderFooter(yeMei, pageSize);
        writer.setPageEvent(headerFooter);
    }


    public static PdfPCell getCell(String title, Font font, int miniMumHeight, int alignMiddle) {
        PdfPCell pdfPCell;
        pdfPCell = new PdfPCell(new Phrase(title, font));
        pdfPCell.setMinimumHeight(miniMumHeight);
        pdfPCell.setVerticalAlignment(alignMiddle);
        return pdfPCell;
    }

    public static PdfPCell getCell(String title, Font font, int miniMumHeight) {
        PdfPCell pdfPCell;
        pdfPCell = new PdfPCell(new Phrase(title, font));
        pdfPCell.setMinimumHeight(miniMumHeight);
        return pdfPCell;
    }

    public static PdfPCell getCell(String title, Font font, int miniMumHeight, boolean useAscender, int horizontalAlignment, int verticalAlignment) {
        PdfPCell pdfPCell;
        pdfPCell = new PdfPCell(new Phrase(title, font));
        pdfPCell.setUseAscender(useAscender);
        pdfPCell.setMinimumHeight(miniMumHeight);
        pdfPCell.setVerticalAlignment(verticalAlignment);
        pdfPCell.setHorizontalAlignment(horizontalAlignment);
        return pdfPCell;
    }

    public static PdfPCell getCell(String title, Font font, int miniMumHeight, boolean useAscender, int horizontalAlignment, int verticalAlignment, int colSpan) {
        PdfPCell pdfPCell;
        pdfPCell = new PdfPCell(new Phrase(title, font));
        pdfPCell.setUseAscender(useAscender);
        pdfPCell.setMinimumHeight(miniMumHeight);
        pdfPCell.setVerticalAlignment(verticalAlignment);
        pdfPCell.setHorizontalAlignment(horizontalAlignment);
        pdfPCell.setColspan(colSpan);
        return pdfPCell;
    }

    public static PdfPCell getCell(String title, Font font, Integer miniMumHeight, Boolean useAscender, Integer horizontalAlignment, Integer verticalAlignment, Integer colSpan, Integer rowSpan, Integer border, Integer borderWidthTop, Integer borderWidthLeft, Integer borderWidthRight) {
        Phrase phrase = new Phrase(title, font);
        PdfPCell pdfPCell = new PdfPCell(phrase);
        if (Objects.nonNull(miniMumHeight)) {
            pdfPCell.setMinimumHeight(miniMumHeight);
        }
        if (Objects.nonNull(useAscender)) {
            pdfPCell.setUseAscender(useAscender);
        }
        if (Objects.nonNull(horizontalAlignment)) {
            pdfPCell.setHorizontalAlignment(horizontalAlignment);
        }
        if (Objects.nonNull(verticalAlignment)) {
            pdfPCell.setVerticalAlignment(verticalAlignment);
        }
        if (Objects.nonNull(colSpan)) {
            pdfPCell.setColspan(colSpan);
        }
        if (Objects.nonNull(rowSpan)) {
            pdfPCell.setRowspan(rowSpan);
        }
        if (Objects.nonNull(border)) {
            pdfPCell.setBorder(border);
        }
        if (Objects.nonNull(borderWidthTop)) {
            pdfPCell.setBorderWidthTop(borderWidthTop);
        }
        if (Objects.nonNull(borderWidthLeft)) {
            pdfPCell.setBorderWidthLeft(borderWidthLeft);
        }
        if (Objects.nonNull(borderWidthRight)) {
            pdfPCell.setBorderWidthRight(borderWidthRight);
        }
        return pdfPCell;
    }
}
